using System;
using System.Collections.Generic;
using System.Text;

namespace Eve.Data
{
    public class Group : EveProperty
    {
        private int categoryId;
        private string groupName;
        private string description;
        private int graphicId;
        private bool useBasePrice;
        private bool allowManufacture;
        private bool allowRecycler;
        private bool anchored;
        private bool anchorable;
        private bool fittableNonSingleton;
        private bool published; // rev 2.0 only

        private Category category;
        private Graphic graphic;
        private List<Type> types;

        public Category Category
        {
            get
            {
                if (category == null)
                    category = new Category(db, categoryId);
                return category;
            }
        }
        public string Name
        {
            get { return groupName; }
        }
        public string Description
        {
            get { return description; }
        }
        public Graphic Graphic
        {
            get
            {
                if (graphic == null)
                    graphic = new Graphic(db, graphicId);
                return graphic;
            }
        }
        public bool UseBasePrice
        {
            get { return useBasePrice; }
        }
        public bool AllowManufacture
        {
            get { return allowManufacture; }
        }
        public bool AllowRecycler
        {
            get { return allowRecycler; }
        }
        public bool Anchored
        {
            get { return anchored; }
        }
        public bool Anchorable
        {
            get { return anchorable; }
        }
        public bool FittableNonSingleton
        {
            get { return fittableNonSingleton; }
        }
        public List<Type> Types
        {
            get
            {
                if (types == null)
                {
                    types = new List<Type>();
                    string query = "SELECT * FROM invTypes WHERE groupID=" + Id;
                    foreach (DbDataRow row in db.ExecuteQuery(query))
                        types.Add(new Type(row));
                }
                return types;
            }
        }
        public bool Published
        {
            get { return published; }
        } // rev 2.0 only

        internal Group(DbDataRow dr)
            : base(dr.Database, EvePropertyType.Group, dr["groupID"])
        {
            Fill(dr);
        }
        internal Group(Database db, int id)
            : base(db, EvePropertyType.Group, id)
        {
            Fill(db.ExecuteSingle("SELECT * FROM invGroups WHERE groupID=" + id));
        }

        private void Fill(DbDataRow dr)
        {
            categoryId = dr["categoryID"];
            groupName = dr["groupName"];
            description = dr["description"];
            graphicId = dr["graphicID"];
            useBasePrice = dr["useBasePrice"];
            allowManufacture = dr["allowManufacture"];
            allowRecycler = dr["allowRecycler"];
            anchored = dr["anchored"];
            anchorable = dr["anchorable"];
            fittableNonSingleton = dr["fittableNonSingleton"];
            published = dr["published"];
        }
    }
}
